package com.huatai.datadevelop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.datadevelop.constant.Constant;
import com.huatai.datadevelop.entity.ApiSuperviseEntity;
import com.huatai.datadevelop.mapper.ApiSuperviseMapper;
import com.huatai.datadevelop.utils.PageUtils;
import com.huatai.datadevelop.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hecai
 * @description: TODO
 * @date 2023/2/9 15:58
 * @Version 1.0
 */
@Service
public class ApiSuperviseService extends ServiceImpl<ApiSuperviseMapper, ApiSuperviseEntity> {

    /**
     * 检测数据异步入库
     * @param apiSuperviseEntity
     */
    @Async("asyncServiceExecutor")
    public void executorService(ApiSuperviseEntity apiSuperviseEntity){
        this.saveOrUpdate(apiSuperviseEntity);
    }

    /**
     * 获取接口错误列表
     * @return
     */
    public List<ApiSuperviseEntity> getSupersionAbnormal(){
        QueryWrapper<ApiSuperviseEntity> qw = new QueryWrapper<>();
        qw.eq("status", Constant.ABNORMAL);
        List<ApiSuperviseEntity> list = this.list(qw);
        return list;
    }

    /**
     * 统计正常正常数量 异常数量
     * @return
     */
    public HashMap<String,Object> getSupersionCount(){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<ApiSuperviseEntity> list = this.list();
        hashMap.put("normal",list.stream().filter(p->p.getStatus().equals(Constant.NORMAL)).count());
        hashMap.put("abnormal",list.stream().filter(p->p.getStatus().equals(Constant.ABNORMAL)).count());
        return hashMap;
    }

    /**
     * 统计正常正常数量 异常数量
     * @return
     */
    public List<String> getTypeList(){
        List<ApiSuperviseEntity> list = this.list();
        return list.stream().map(p->p.getType()).distinct().collect(Collectors.toList());
    }

    /**
     * 根据状态获取该状态下相关统计数据
     * @param status
     * @return
     */
    public List<HashMap<String,Integer>> getCountDataByType(Integer status){
        List<HashMap<String, Integer>> countDataByType = this.baseMapper.getCountDataByType(status);
        return countDataByType;
    }

    /**
     * 分页获取数据
     *
     * @param params
     * @return
     */
    public PageUtils queryPage(Map<String, Object> params) {
        Integer status = (Integer) params.get("status");
        String type = (String) params.get("type");
        IPage<ApiSuperviseEntity> page = this.page(
                new Query<ApiSuperviseEntity>().getPage(params),
                new QueryWrapper<ApiSuperviseEntity>()
                        .eq(status!=null, "status", status)
                        .eq(StringUtils.isNotBlank(type), "type", type)
        );
        return new PageUtils(page);
    }
}
